﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Entity;
using Utility;

namespace DAL
{
    public class UserDAO
    {

        public static string GetLatestUserIdByYearTail(string yearTail)
        {
            string sql = SqlSentence.getLatestUserIdByYearTail;
            SqlParameter[] parameter =
            {
                new SqlParameter("@yearTail", yearTail+"%")
            };
            return SqlHelper.ExecuteScalarText(sql, parameter) as string;
        }

        public static int InsertUser(User user)
        {
            string sql = SqlSentence.insertUser;
            SqlParameter[] parameter =
            {
                new SqlParameter("@UserId", user.UserId),
                new SqlParameter("@UserName", user.UserName),
                new SqlParameter("@Password", user.Password),
                new SqlParameter("@Type", user.Type),
                new SqlParameter("@Email", user.Email),
                new SqlParameter("@Telephone", user.Telephone),
                new SqlParameter("@Cellphone", user.Cellphone)                             
            };
            return SqlHelper.ExecteNonQueryText(sql, parameter);
        }

        public static DataTable GetUserTableByUserName(string userName)
        {
            string sql = SqlSentence.getUserTableByUserName;
            SqlParameter[] parameter =
            {
                new SqlParameter("@UserName", userName)
            };
            return SqlHelper.GetSingleTableText(sql, parameter);
        }

        public static int GetIdByUserId(string userId)
        {
            string sql = SqlSentence.getIdByUserId;
            SqlParameter[] parameter =
            {
                new SqlParameter("@UserId", userId)
            };

            object obj = SqlHelper.GetSingle(sql, parameter);
            if (null != obj)
            {
                return (int) obj;
            }
            else
            {
                return -1;
            }
        }

        public static DataTable GetUserById(int id)
        {
            string sql = SqlSentence.getUserById;
            SqlParameter[] parameter =
            {
                new SqlParameter("@Id", id)
            };
            return SqlHelper.GetSingleTableText(sql, parameter);
        }
    }
}
